<?php
namespace app\admin\controller;

use app\admin\model\AuthRule as AuthRules;
use app\admin\validate\AuthRule as cValidate;
use think\Request;

class AuthRule extends Common
{
    private $cModel;   //当前控制器关联模型
    private $validate;   //当前控制器关联验证类
    private $module = 'admin';

    public function initialize()
    {
        parent::initialize();
        $this->cModel = new AuthRules;   //别名：避免与控制名冲突
        $this->validate=new cValidate;   //创建一个验证类
    }
    
    public function index()
    {
        $dataList = $this->cModel->treeList();
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    //获取所有权限列表树，添加用户时调用
    public function tree()
    {
        return json($this->cModel->treeList3());
    }
//    //资源节点创建7个节点（控制器，控制器/index，控制器/tree，控制器/add，控制器/edit，控制器/delete，控制器/ajax）
//    public function resource(Request $request)
//    {
//        if ($request->isPost()){
//            $data = input('post.');
//            if (! $this->validate->scene('resource')->check($data)) {
//                $this->result(null,400,$this->validate->getError());
//            }
//            $result = $this->cModel->insert(['']);
//            if ($result){
//                return json(['code'=>0,'msg'=>'成功']);
//            }else{
//                return json(['code'=>400,'msg'=>'保存失败']);
//            }
//        }
//        return $this->fetch();
//    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $data = input('post.');
            if (! $this->validate->scene('add')->check($data)) {
                $this->result(null,400,$this->validate->getError());
            }
            $result = $this->cModel->allowField(true)->create($data);
            if ($result){
                return json(['code'=>0,'msg'=>'成功']);
            }else{
                return json(['code'=>400,'msg'=>'保存失败']);
            }
        }
        $treeList = $this->cModel->treeList($this->module);
        $this->assign('treeList', $treeList);
        return $this->fetch();
    }
    
    public function edit(Request $request,$id)
    {
        if ($request->isPost()){
            $data = input('post.');
            if (! $this->validate->scene('edit')->check($data)) {
                $this->result(null,400,$this->validate->getError());
            }
            $this->cModel=$this->cModel->get($id);
            $result = $this->cModel->save($data);
            if ($result){
                return json(['code' => 0, 'msg' => '成功', 'data' => null]);
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $data = $this->cModel->get($id);
            $this->assign('data', $data);
            $this->assign('module', $this->module);
            $treeList = $this->cModel->treeList($this->module);
            $this->assign('treeList', $treeList);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        if (request()->isPost()) {
            if(is_array($id)){//批量删除

            }else{//单选删除
                $list=$this->cModel->treeList2($id);//获取该节点下所有节点数组
                $id=[$id];
                if($list){//存在子节点则合并
                    $id=array_merge($id,array_column($list,'id'));
                }
            }
            //级联删除该id下的所有节点
            $result = AuthRules::destroy($id);
            if ($result) {
                return json(['code' => 0, 'msg' => '成功删除'.count($id).'条记录', 'data' => null]);
            } else {
                return json(['code' => 404, 'msg' => '删除失败', 'data' => null]);
            }
        }
    }

    /*
    * ajax更新部分数据
    */
    public function ajax(Request $request, $id)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $validate = new cValidate;
            if (!$validate->scene('ajax')->check($data)) {
                return json(['code' => 404, 'msg' => $validate->getError(), 'data' => null]);
            }
            $auth_rule = AuthRules::get($id);
            $auth_rule[key($data)]=$auth_rule[key($data)]?0:1;
//            $result = $auth_rule->save([key($data)=>$auth_rule[key($data)]?0:1]);
            $result = $auth_rule->save();
            if ($result) {
                return json(['code' => 0, 'msg' => '成功', 'data' => null]);
            } else {
                return json(['code' => 404, 'msg' => '更新失败', 'data' => null]);
            }
        } else {
            return json(['code' =>404, 'msg' => '请求不被允许', 'data' => null]);
        }
    }
}